package com.lanou.connotationjoke.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.lanou.connotationjoke.view.NewListView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.WebViewActivity;
import com.lanou.connotationjoke.adapter.HomeEliteListViewAdapter;
import com.lanou.connotationjoke.entity.AppUrl;
import com.lanou.connotationjoke.entity.DataDetialEty;
import com.lanou.connotationjoke.entity.EliteEty;
import com.lanou.connotationjoke.tool.MyAppllication;
import com.lanou.connotationjoke.tool.VolleySingleton;

/**
 * Created by dllo on 16/1/8.
 */
public class HomeEliteFragment extends BaseFragment{
    private NewListView listView;
    private HomeEliteListViewAdapter adapter;
    private String URL = AppUrl.HOMEELITE;
    private EliteEty eliteEty;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_elite,null);
        listView = (NewListView) view.findViewById(R.id.listview_home_elite);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        VolleySingleton.addRequest(URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        String result = response.substring(7, response.lastIndexOf(")"));
                        Gson gson = new Gson();
                        eliteEty = gson.fromJson(result, EliteEty.class);
                        DataDetialEty dataDetialEty = eliteEty.getData().get(0);


                        View headView = null;
                        try {
                            headView = LayoutInflater.from(getActivity()).
                                    inflate(R.layout.headview_listview_home_elite, null);
                        } catch (NullPointerException e) {
                            e.printStackTrace();
                        }


                        TextView dateTimeTV = (TextView) headView.findViewById(R.id.tv_datetime);
                        TextView titleTv = (TextView) headView.findViewById(R.id.tv_title);
                        NetworkImageView headIv = (NetworkImageView) headView.findViewById(R.id.iv_url);
                        String date = dataDetialEty.getDatetime();
                        date = date.substring(6, 10);
                        date = date.replace("-", "月");
                        date = date + "日";
                        dateTimeTV.setText(date);
                        titleTv.setText(dataDetialEty.getTitle());
                        headIv.setErrorImageResId(R.mipmap.ic_launcher);
                        headIv.setDefaultImageResId(R.mipmap.ic_launcher);
                        headIv.setImageUrl(dataDetialEty.getImage_list().get(0).getUrl(), VolleySingleton.getImageLoader());
                        listView.addHeaderView(headView);
                        eliteEty.getData().remove(0);
                        adapter = new HomeEliteListViewAdapter(getActivity(), eliteEty.getData());
                        listView.setAdapter(adapter);


                        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
                            @Override
                            public void onScrollStateChanged(AbsListView absListView, int i) {
                                switch (i) {
                                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://当不迁移转变时
                                        //断定迁移转变到底部
                                        if (absListView.getLastVisiblePosition() == (absListView.getCount() - 1)) {
                                            Toast.makeText(getActivity(), "加载", Toast.LENGTH_SHORT).show();
                                            VolleySingleton.addRequest(URL, new Response.Listener<String>() {
                                                @Override
                                                public void onResponse(String response) {
                                                    String result = response.substring(7, response.lastIndexOf(")"));
                                                    Gson gson = new Gson();
                                                    EliteEty eliteEtyNew = null;
                                                    eliteEtyNew = gson.fromJson(result, EliteEty.class);
                                                    eliteEty.getData().addAll(eliteEtyNew.getData());
                                                    adapter.refreshData(eliteEty.getData());
                                                }
                                            }, new Response.ErrorListener() {
                                                @Override
                                                public void onErrorResponse(VolleyError error) {

                                                }
                                            });

                                        }
                                        break;

                                }
                            }

                            @Override
                            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                            }
                        });


                    }
                }

                , new Response.ErrorListener()

                {
                    @Override
                public void onErrorResponse(VolleyError error) {

                    }
                }

        );


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()

                                        {
                                            @Override
                                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                if (MyAppllication.hasNetwork() && position != 0) {
                                                    String url = eliteEty.getData().get(position - 1).getSource_url();
                                                    WebViewActivity.goToWebViewActivity(getActivity(), url);
                }
            }
            }

            );


        }


    }
